<?php

	echo "<script language='javascript'>

	function mostrarAba(id) {
		abaVisivel = document.getElementById('AssistidoAbaVisivel').value;
		document.getElementById('aba'+abaVisivel).style.display = 'none';

		document.getElementById('AssistidoAbaVisivel').value = id;
		document.getElementById('aba'+id).style.display = '';


		/* Modifica a cor da aba */
		document.getElementById('atab'+abaVisivel).className = 'tabBegin';
		document.getElementById('litab'+abaVisivel).className = 'tabEnd';

		document.getElementById('atab'+id).className = 'tabBeginSelected';
		document.getElementById('litab'+id).className = 'tabEndSelected';
	}

	</script>";
	echo $html->css('fd-datepicker');
	echo $javascript->link('datepicker.js');
	echo $accessibility->whereIAm($pageTitle);
	echo "<h2 id='contentTitle'>".$pageTitle."</h2>";

	echo $this->renderElement('message');

	if (isset($assistido) && is_array($assistido) && (!empty($assistido))) {

	/* Aba - Begin */
	echo "<div id='tab' class='tab'>";
	echo "<ul>";
	echo "<li id='litab0' class='tabEndSelected' style='padding-bottom: 6px;'>";
	echo "<a id='atab0' class='tabBeginSelected' href='javascript:mostrarAba(0); '>".
		 "<span  id='spantab0'>Dados</span></a>";
	echo "</li>";
	$k = 1;
	foreach ($formularios as $formulario) {
		echo "<li id='litab".$k."' class='tabEnd' style='padding-bottom: 6px;'>";
		$span = "<span id='spantab".$k."' >";
		echo "<a id='atab".$k."' class='tabBegin' href='javascript:mostrarAba(".$k.");'>".
			 $span.$formulario['Formulario']['name']."</span></a>\n";
		echo "</li>";
		$k++;
	}
	echo "</ul>";
	echo "</div>";
	/* Aba - End */

		echo $form->create(null,array('type' => 'file'));
		echo "<div id='aba0'>";
		echo $form->hidden('abaVisivel', array('value'=>0));
		echo $form->hidden('id');
		echo $form->input('nome',array('label'=>$accessibility->required('Nome')));
		echo $form->input('endereco',array('label'=>'Endereço'));
		echo $form->input('bairro',array('label'=>'Bairro'));
		echo $form->input('rg',array('label'=>'RG'));
		echo $form->input('cpf',array('label'=>'CPF'));
		//echo $form->input('birthdate',array('label'=>'Nascimento'));
		echo $form->label('Assistido.birthdate','Nascimento');
		echo $input->date('Assistido','birthdate',null);
		echo $form->label('unidade_id',$accessibility->required('Unidade'));
		echo $form->select('unit_id', $units, null, array())."<br/>";
		echo $input->city('edit', 'Assistido', $estados, $estado_selected, $cidades, $assistido['Assistido']['cidade_id']);
		// Upload da imagem do usuário
		echo "<div class='input'>";
		echo $form->label('photo','Foto');
		echo $form->file('photo');
		if (!empty($assistido['Assistido']['photo'])) {
			echo $html->image('../assistidos/photo/'.$assistido['Assistido']['id'],array('class'=>'userPhoto'));
		}
		echo "<br/>";
		echo "</div>";

		echo "</div>";

		$iFvalor = 0;
		$iFvalorText = 0;
		$iFvalorFile = 0;
		$j = 1;

		foreach ($formularios as $formulario) {
			echo "<div id='aba".$j."' style='display:none;'>";
			echo "<h3>".$formulario['Formulario']['name']."</h3>";
			foreach ($formulario["Fcampo"] as $campo) {

				switch ($campo["type"]) {
					case 'string':
						if (!isset($Fvalores[$campo["id"]])) $Fvalores[$campo["id"]] = array('Fvalor'=>array('id'=>null,'string'=>''));
						echo $form->hidden('Fvalor_'.$iFvalor.'.fcampo_id', array("value"=>$campo["id"]));
						echo $form->hidden('Fvalor_'.$iFvalor.'.id', array("value"=>$Fvalores[$campo["id"]]['Fvalor']['id']));
						echo $form->input('Fvalor_'.$iFvalor.'.string',
							array('value'=>$Fvalores[$campo["id"]]['Fvalor']['string'],'label'=>$campo["name"]));
						$iFvalor++;
						break;
					case 'inteiro':
						if (!isset($Fvalores[$campo["id"]])) $Fvalores[$campo["id"]] = array('Fvalor'=>array('id'=>null,'inteiro'=>''));
						echo $form->hidden('Fvalor_'.$iFvalor.'.fcampo_id', array("value"=>$campo["id"]));
						echo $form->hidden('Fvalor_'.$iFvalor.'.id', array("value"=>$Fvalores[$campo["id"]]['Fvalor']['id']));
						echo $form->input('Fvalor_'.$iFvalor.'.inteiro',
							array('value'=>$Fvalores[$campo["id"]]['Fvalor']['inteiro'],'label'=>$campo["name"]));
						$iFvalor++;
						break;
					case 'data':
						if (!isset($Fvalores[$campo["id"]])) $Fvalores[$campo["id"]] = array('Fvalor'=>array('id'=>null,'data'=>''));
						echo $form->hidden('Fvalor_'.$iFvalor.'.fcampo_id', array("value"=>$campo["id"]));
						echo $form->hidden('Fvalor_'.$iFvalor.'.id', array("value"=>$Fvalores[$campo["id"]]['Fvalor']['id']));
						echo $form->input('Fvalor_'.$iFvalor.'.data',
							array('value'=>$Fvalores[$campo["id"]]['Fvalor']['data'],'label'=>$campo["name"]));
						$iFvalor++;
						break;
					case 'texto':
						if (!isset($Fvalores[$campo["id"]])) $Fvalores[$campo["id"]] = array('Fvalortext'=>array('id'=>null,'texto'=>''));
						echo $form->hidden('FvalorText_'.$iFvalorText.'.fcampo_id', array("value"=>$campo["id"]));
						echo $form->hidden('FvalorText_'.$iFvalorText.'.id', array("value"=>$Fvalores[$campo["id"]]['Fvalortext']['id']));
						echo $form->label('FvalorText_'.$iFvalorText.'.texto',$campo["name"]);
						echo $form->textarea('FvalorText_'.$iFvalorText.'.texto',
							array('value'=>$Fvalores[$campo["id"]]['Fvalortext']['texto']));
						$iFvalorText++;
						break;
					case 'file':
						if (!isset($Fvalores[$campo["id"]])) $Fvalores[$campo["id"]] = array('Fvalor'=>array('id'=>null,'string'=>''));
						echo "<div class='input'>";
						echo $form->hidden('FvalorFile_'.$iFvalorFile.'.fcampo_id', array("value"=>$campo["id"]));
						echo $form->hidden('FvalorFile_'.$iFvalorFile.'.id', array("value"=>$Fvalores[$campo["id"]]['Fvalor']['id']));
						echo $form->label('FvalorFile_'.$iFvalorFile.'.file',$campo["name"]);
						echo $Fvalores[$campo["id"]]['Fvalor']['string']."<br/>";
						echo $form->file('FvalorFile_'.$iFvalorFile.'.file')."<br/>";
						echo "</div>";
						$iFvalorFile++;
						break;
				}
			}
			$j++;
			echo "</div>";
		}
		echo "<br/>";
		echo $form->end('Salvar alterações');


	} else {
		echo "<p>Não existe assistido cadastrado com o id fornecido</p>";
	}
